package com.example.algorithm.algorithmService;

import com.example.algorithm.enums.MonthsEnum;
import com.example.algorithm.mapper.ResultIndexMarkMonthsMapper;
import com.example.algorithm.mapper.SvYdNumberMonthMapper;
import com.example.algorithm.pojo.SvYdNumberMonthDO;
import com.example.algorithm.result.ResultIndexMark;
import com.example.algorithm.result.ResultIndexMarkMonths;
import com.example.algorithm.pojo.TimeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ControlPowerByMonthService {
    @Autowired
    ControlAlgorithmService controlAlgorithmService;

    @Resource
    private SvYdNumberMonthMapper numberMonthMapper;

    @Autowired
    ResultIndexMarkMonthsMapper resultIndexMarkMonthsMapper;


    ResultIndexMarkMonths resultIndexMarkMonths = new ResultIndexMarkMonths();

    public void getResultByMonth() {
        HashMap<Date, ResultIndexMark> resultMap = controlAlgorithmService.getResultMap();
        ArrayList<ResultIndexMark> januaryList = new ArrayList<>();
        ArrayList<ResultIndexMark> februaryList = new ArrayList<>();
        ArrayList<ResultIndexMark> marchList = new ArrayList<>();
        ArrayList<ResultIndexMark> aprilList = new ArrayList<>();
        ArrayList<ResultIndexMark> mayList = new ArrayList<>();
        ArrayList<ResultIndexMark> juneList = new ArrayList<>();
        ArrayList<ResultIndexMark> julyList = new ArrayList<>();
        ArrayList<ResultIndexMark> augustList = new ArrayList<>();
        ArrayList<ResultIndexMark> septemberList = new ArrayList<>();
        ArrayList<ResultIndexMark> octoberList = new ArrayList<>();
        ArrayList<ResultIndexMark> novemberList = new ArrayList<>();
        ArrayList<ResultIndexMark> decemberList = new ArrayList<>();
        for (Date calculationDate : resultMap.keySet()) {
            ResultIndexMark resultIndexMark = resultMap.get(calculationDate);
            TimeInfo timeInfo = null;
            try {
                timeInfo = getTime(calculationDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Integer month = timeInfo.getMonth();
            if (MonthsEnum.January.getValue() <= month && month < MonthsEnum.February.getValue()) {
                januaryList.add(resultIndexMark);
            } else if (MonthsEnum.February.getValue() <= month && month < MonthsEnum.March.getValue()) {
                februaryList.add(resultIndexMark);
            } else if (MonthsEnum.March.getValue() <= month && month < MonthsEnum.April.getValue()) {
                marchList.add(resultIndexMark);
            } else if (MonthsEnum.April.getValue() <= month && month < MonthsEnum.May.getValue()) {
                aprilList.add(resultIndexMark);
            } else if (MonthsEnum.May.getValue() <= month && month < MonthsEnum.June.getValue()) {
                mayList.add(resultIndexMark);
            } else if (MonthsEnum.June.getValue() <= month && month < MonthsEnum.July.getValue()) {
                juneList.add(resultIndexMark);
            } else if (MonthsEnum.July.getValue() <= month && month < MonthsEnum.August.getValue()) {
                julyList.add(resultIndexMark);
            } else if (MonthsEnum.August.getValue() <= month && month < MonthsEnum.September.getValue()) {
                augustList.add(resultIndexMark);
            } else if (MonthsEnum.September.getValue() <= month && month < MonthsEnum.October.getValue()) {
                septemberList.add(resultIndexMark);
            } else if (MonthsEnum.October.getValue() <= month && month < MonthsEnum.November.getValue()) {
                octoberList.add(resultIndexMark);
            } else if (MonthsEnum.November.getValue() <= month && month < MonthsEnum.December.getValue()) {
                novemberList.add(resultIndexMark);
            } else if (MonthsEnum.December.getValue() <= month && month < MonthsEnum.January.getValue()) {
                decemberList.add(resultIndexMark);
            }
        }
        if (januaryList.size() != 0) {
            getJanuaryResult(januaryList);
        }
        if (februaryList.size() != 0) {
            getFebruaryResult(februaryList);
        }
        if (marchList.size() != 0) {
            getMarchResult(marchList);
        }
        if (aprilList.size() != 0) {
            getAprilResult(aprilList);
        }
        if (mayList.size() != 0) {
            getMayResult(mayList);
        }
        if (juneList.size() != 0) {
            getJuneResult(juneList);
        }
        if (julyList.size() != 0) {
            getJulyResult(julyList);
        }
        if (augustList.size() != 0) {
            getAugust(augustList);
        }
        if (septemberList.size() != 0) {
            getSeptemberResult(septemberList);
        }
        if (octoberList.size() != 0) {
            getOctober(octoberList);
        }
        if (novemberList.size() != 0) {
            getNovemberResult(novemberList);
        }
        if (decemberList.size() != 0) {
            getDecemberResult(decemberList);
        }
    }

    private void getDecemberResult(ArrayList<ResultIndexMark> decemberList) {
        getAvgResult(decemberList);
    }

    private void getNovemberResult(ArrayList<ResultIndexMark> novemberList) {
        getAvgResult(novemberList);
    }

    private void getOctober(ArrayList<ResultIndexMark> octoberList) {
        getAvgResult(octoberList);
    }

    private void getSeptemberResult(ArrayList<ResultIndexMark> septemberList) {
        getAvgResult(septemberList);
    }

    private void getAugust(ArrayList<ResultIndexMark> augustList) {
        getAvgResult(augustList);
    }

    private void getJulyResult(ArrayList<ResultIndexMark> julyList) {
        getAvgResult(julyList);
    }

    private void getJuneResult(ArrayList<ResultIndexMark> juneList) {
        getAvgResult(juneList);
    }

    private void getMayResult(ArrayList<ResultIndexMark> mayList) {
        getAvgResult(mayList);
    }

    private void getAprilResult(ArrayList<ResultIndexMark> aprilList) {
        getAvgResult(aprilList);
    }

    private void getMarchResult(ArrayList<ResultIndexMark> marchList) {
        getAvgResult(marchList);
    }

    private void getFebruaryResult(ArrayList<ResultIndexMark> februaryList) {
        getAvgResult(februaryList);
    }

    private void getJanuaryResult(ArrayList<ResultIndexMark> januaryList) {
        getAvgResult(januaryList);
    }


    private void getAvgResult(ArrayList<ResultIndexMark> list) {
        // 清空数据
        numberMonthMapper.delete(null);

        double provinceStockAvg = 0d;
        OptionalDouble average = list.stream().mapToDouble(ResultIndexMark::getProvinceStockAvg).average();
        if (average.isPresent()) {
            provinceStockAvg = average.getAsDouble();
        }

        double cityStockAvg = 0d;
        OptionalDouble average1 = list.stream().mapToDouble(ResultIndexMark::getCityStockAvg).average();
        if (average1.isPresent()) {
            cityStockAvg = average1.getAsDouble();
        }

        double proIncrementAvg = 0d;
        OptionalDouble average2 = list.stream().mapToDouble(ResultIndexMark::getProIncrementAvg).average();
        if (average2.isPresent()) {
            proIncrementAvg = average2.getAsDouble();
        }

        double cityIncrementAvg = 0d;
        OptionalDouble average3 = list.stream().mapToDouble(ResultIndexMark::getCityIncrementAvg).average();
        if (average3.isPresent()) {
            cityIncrementAvg = average3.getAsDouble();
        }

        double proCityStockAvg = 0d;
        OptionalDouble average4 = list.stream().mapToDouble(ResultIndexMark::getProCityStockAvg).average();
        if (average4.isPresent()) {
            proCityStockAvg = average4.getAsDouble();
        }

        double proCityIncrementAvg = 0d;
        OptionalDouble average5 = list.stream().mapToDouble(ResultIndexMark::getProCityIncrementAvg).average();
        if (average5.isPresent()) {
            proCityIncrementAvg = average5.getAsDouble();
        }
        double provinceControlPower = 0d;
        OptionalDouble average14 = list.stream().mapToDouble(ResultIndexMark::getProvinceControlPower).average();
        if (average14.isPresent()) {
            provinceControlPower = average14.getAsDouble();
        }
        double cityControlPower = 0d;
        OptionalDouble average6 = list.stream().mapToDouble(ResultIndexMark::getCityControlPower).average();
        if (average6.isPresent()) {
            cityControlPower = average6.getAsDouble();
        }


        double sumIndexMark = 0d;
        OptionalDouble average7 = list.stream().mapToDouble(ResultIndexMark::getSumIndexMark).average();
        if (average7.isPresent()) {
            sumIndexMark = average7.getAsDouble();
        }

        double immediateChangeScoreResult = 0d;
        OptionalDouble average8 = list.stream().mapToDouble(ResultIndexMark::getImmediateChangeScoreResult).average();
        if (average8.isPresent()) {
            immediateChangeScoreResult = average8.getAsDouble();
        }

        double reformGoodScoreResult = 0d;
        OptionalDouble average9 = list.stream().mapToDouble(ResultIndexMark::getReformGoodScoreResult).average();
        if (average9.isPresent()) {
            reformGoodScoreResult = average9.getAsDouble();
        }
        double activeDiscoveryScoreResult = 0d;
        OptionalDouble average10 = list.stream().mapToDouble(ResultIndexMark::getActiveDiscoveryScoreResult).average();
        if (average10.isPresent()) {
            activeDiscoveryScoreResult = average10.getAsDouble();
        }

        double drawInferencesScoreResult = 0d;
        OptionalDouble average11 = list.stream().mapToDouble(ResultIndexMark::getDrawInferencesScoreResult).average();
        if (average11.isPresent()) {
            drawInferencesScoreResult = average11.getAsDouble();
        }


        double resultScore = 0d;
        OptionalDouble average13 = list.stream().mapToDouble(ResultIndexMark::getResultScore).average();
        if (average13.isPresent()) {
            resultScore = average13.getAsDouble();
        }

        List<Date> dateList = list.stream().map(ResultIndexMark::getCalculationDate).collect(Collectors.toList());
        TimeInfo timeInfo = null;
        Integer year = 0;
        Integer month = 0;

        for (Date date : dateList) {
            try {
                timeInfo = getTime(date);
                year = timeInfo.getYear();
                month = timeInfo.getMonth();

            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        resultIndexMarkMonths.setCalculationDateMonth(year + "年" + month + "月");
        resultIndexMarkMonths.setProvinceStockAvgMonth(provinceStockAvg);
        resultIndexMarkMonths.setCityStockAvgMonth(cityStockAvg);
        resultIndexMarkMonths.setProIncrementAvgMonth(proIncrementAvg);
        resultIndexMarkMonths.setCityIncrementAvgMonth(cityIncrementAvg);
        resultIndexMarkMonths.setProCityStockAvgMonth(proCityStockAvg);
        resultIndexMarkMonths.setProCityIncrementAvgMonth(proCityIncrementAvg);
        resultIndexMarkMonths.setProvinceControlPowerMonth(provinceControlPower);
        resultIndexMarkMonths.setCityControlPowerMonth(cityControlPower);
        resultIndexMarkMonths.setSumIndexMarkMonth(sumIndexMark);
        resultIndexMarkMonths.setImmediateChangeScoreResultMonth(immediateChangeScoreResult);
        resultIndexMarkMonths.setReformGoodScoreResultMonth(reformGoodScoreResult);
        resultIndexMarkMonths.setActiveDiscoveryScoreResultMonth(activeDiscoveryScoreResult);
        resultIndexMarkMonths.setDrawInferencesScoreResultMonth(drawInferencesScoreResult);
      //  resultIndexMarkMonths.setSurfaceControlScoreResultMonth(surfaceControlScoreResult);
        resultIndexMarkMonths.setResultScoreMonth(resultScore);
        for (ResultIndexMark resultIndexMark : list) {
            try {
                Integer day= getTime(resultIndexMark.getCalculationDate()).getDay();
                if (day==15){
                    resultIndexMarkMonths.setMiddleStockScoreMonth(resultIndexMark.getProCityStockAvg());
                    resultIndexMarkMonths.setMiddleIncreaseScoreMonth(resultIndexMark.getProCityIncrementAvg());
                    resultIndexMarkMonths.setMiddleListScoreMonth(resultIndexMark.getSumIndexMark());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //  System.out.println(resultIndexMarkMonths);
        resultIndexMarkMonthsMapper.insert(resultIndexMarkMonths);


        SvYdNumberMonthDO svYdNumberMonthDO = new SvYdNumberMonthDO();
        svYdNumberMonthDO.setMonth(month);
        svYdNumberMonthDO.setYear(year+"");
        svYdNumberMonthDO.setListNu(sumIndexMark);
        svYdNumberMonthDO.setProblemNu(proCityStockAvg);
        svYdNumberMonthDO.setRepeatNu(proCityIncrementAvg);
        svYdNumberMonthDO.setDataValid(1);
        numberMonthMapper.insert(svYdNumberMonthDO);
    }

    public TimeInfo getTime(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dateStr = sdf.format(date);
        Integer year = Integer.parseInt(dateStr.substring(0, 4));
        Integer month = Integer.parseInt(dateStr.substring(5, 7));
        Integer day = Integer.parseInt(dateStr.substring(dateStr.length() -2,dateStr.length()));
        TimeInfo timeInfo = new TimeInfo();
        timeInfo.setYear(year);
        timeInfo.setMonth(month);
        timeInfo.setDay(day);
        return timeInfo;
    }
}
